package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.Acl_Role;
import com.ws.ssmzongjie3.bean.Acl_User;
import com.ws.ssmzongjie3.bean.Acl_User_Role;
import com.ws.ssmzongjie3.bean.ProVo;
import com.ws.ssmzongjie3.service.Acl_RoleService;
import com.ws.ssmzongjie3.service.Acl_User_RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/acl_role")
public class Acl_RoleController {
    @Autowired
    private Acl_RoleService acl_RoleService;
    @Autowired
    private Acl_User_RoleService acl_User_RoleService;
    @RequestMapping("/list")
    public String select(Model model, ProVo proVo, Acl_Role acl_role){
        System.out.println("links:"+acl_role);

        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Acl_Role> aclRoles = acl_RoleService.listAcl_Role(acl_role);
        PageInfo pageInfo = new PageInfo(aclRoles);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("acl_role",acl_role);

        return "WEB-INF/admin/acl_role/list";
    }

    @RequestMapping("/add")
    public String add(){
        return "WEB-INF/admin/acl_role/add";
    }

    @RequestMapping("/insert")
    public String insert(Acl_Role acl_role){
        acl_RoleService.insertAcl_Role(acl_role);
        return "redirect:/acl_role/list";
    }

    @RequestMapping("/delete")
    public String delete(Acl_Role acl_role){
        acl_RoleService.deleteAcl_Role(acl_role);
        return "redirect:/acl_role/list";
    }

    @RequestMapping("/update")
    public String update(Acl_Role acl_role){
        acl_RoleService.updateAcl_Role(acl_role);
        return "redirect:/acl_role/list";
    }

    @RequestMapping("/huixian")
    public String huixian(Acl_Role acl_role, Model model){
        Acl_Role aclRole = acl_RoleService.getAcl_RoleById(acl_role);
        model.addAttribute("aclRole",aclRole);
        return "WEB-INF/admin/acl_role/update";
    }

    @RequestMapping("/role")
    public String role(Model model,  Acl_User acl_user , Acl_Role acl_role){
        List<Integer> integers = acl_RoleService.listRole_id(acl_user);
        List<Acl_Role> aclRoles = acl_RoleService.listAcl_Role(acl_role);
        for (Acl_Role aclRole : aclRoles) {
            if (integers.contains(aclRole.getRole_id())){
                aclRole.setIs_boolean(1);
            }
        }
        model.addAttribute("aclRoles",aclRoles);
        model.addAttribute("acl_user",acl_user);
        return "/WEB-INF/admin/acl_user/role";
    }

    @RequestMapping("/addrole")
    public String role(Model model, Acl_User_Role acl_user_role,Integer [] roleId ){
        acl_User_RoleService.deleteAcl_User_Role(acl_user_role);

        for (Integer integer : roleId){
            acl_user_role.setRole_id(integer);
            acl_User_RoleService.insertAcl_User_Role(acl_user_role);
        }
        return "redirect:/houuser/list";
    }

}
